package cc.vicp.djx314.starfood.web.personnel.order;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cc.vicp.djx314.starfood.domain.order.Order;
import cc.vicp.djx314.starfood.service.order.OrderService;

/**
 * 获取已经取消的订单
 * @author 水山清风
 *
 */
@Controller
public class OrderCancledController {
	public static final Logger logger = LoggerFactory.getLogger(OrderCancledController.class);
	@Autowired
	OrderService orderService;

	@RequestMapping(value = "/personnel/uncancled/order/{orderId}", method = RequestMethod.GET)
	public ModelAndView getCancledOrderById(@PathVariable("orderId") Integer orderId) {
		boolean success = false;
		Order order = null;
		try {
			order = orderService.getOrderCancledById(orderId);
			success = true;
		} catch (Exception e) {
			logger.info("获取已经作废的订单过程出错", e);
		}
		ModelAndView model = new ModelAndView("jsontournamenttemplate");
		model.addObject("success", success);
		model.addObject("order", order);
		return model;
	}

	@RequestMapping(value = "/personnel/uncanceled/orders/today", method = RequestMethod.GET)
	public ModelAndView getCanceledToday() {
		boolean success = false;
		ModelAndView model = new ModelAndView("jsontournamenttemplate");
		try {
			List<Order> orders = orderService.getCanceledOrderToday();
			if (orders == null) {
				orders = new ArrayList<Order>();
			}
			model.addObject("orders", orders);
			success = true;
		} catch (Exception e) {
			logger.info("获取今天作废订单数据发生错误", e);
		}
		model.addObject("success", success);
		return model;
	}

	@RequestMapping(value = "/personnel/uncancled/order/{orderId}", method = RequestMethod.PUT)
	public ModelAndView orderUncancled(@PathVariable("orderId") Integer orderId) {
		ModelAndView model = new ModelAndView("jsontournamenttemplate");
		boolean success = false;
		try {
			orderService.updateOrderUncanceled(orderId);
			success = true;
		} catch (Exception e) {
			logger.info("恢复订单过程出现错误", e);
		}
		model.addObject("success", success);
		return model;
	}
}